package current.cas;
/*
ABA问题解决方案，使用原子引用
 */

import java.util.concurrent.atomic.AtomicReference;

class User {
    String userName;
    int age;

    public User() {
    }

    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                '}';
    }
}

public class AtomicReferenceDemo {

    public static void main(String[] args) {

        User z3 = new User("z3",23);
        User l4 = new User("l4", 25);
        User w5 = new User("w5",26);

        AtomicReference<User> atomicReference = new AtomicReference<>();

        atomicReference.set(z3);

        System.out.println(atomicReference.compareAndSet(z3, l4) +"\t current user :" + atomicReference.get().toString());

        System.out.println(atomicReference.compareAndSet(z3, w5) +"\t current user :" + atomicReference.get().toString());
    }
}

